﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.OpenCode.Framework.DevForm;
using DevExpress.XtraTreeList.Nodes;
using System.Windows.Forms;

namespace Com.OpenCode.Framework.DevForm
{
    public class ThreeStateTreeList : OpenCodeTreeUI
    {
        public ThreeStateTreeList()
        {
            this.OptionsView.ShowCheckBoxes = true;
            this.OptionsView.ShowColumns = false;
            this.OptionsView.ShowVertLines = false;
            this.OptionsView.ShowHorzLines = false;
            this.AfterCheckNode += ThreeStateTreeList_AfterCheckNode;
        }

        private void ThreeStateTreeList_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            if (e.Node != null)
            {
                TreeListNode node = e.Node;
                if (node.Checked)
                {
                    if (node.HasChildren)
                    {
                        node.CheckAll();
                    }
                }
                else
                {
                    if (node.HasChildren)
                    {
                        node.UncheckAll();
                    }
                }
                SetParentNodeState(node);
            }
        }

        /// <summary>
        /// 设置父节点的节点状态。
        /// </summary>
        /// <param name="node">当前勾选的节点。</param>
        private void SetParentNodeState(TreeListNode node)
        {
            TreeListNode parentNode = node.ParentNode;
            if (parentNode != null && parentNode.HasChildren)
            {
                int count = 0;
                for (int i = 0; i < parentNode.Nodes.Count; i++)
                {
                    if (parentNode.Nodes[i].CheckState == CheckState.Checked)
                    {
                        count++;
                    }
                    if (parentNode.Nodes[i].CheckState == CheckState.Indeterminate)
                    {
                        count = -1;
                        break;
                    }
                }
                if (count == parentNode.Nodes.Count)
                {
                    parentNode.CheckState = CheckState.Checked;
                }
                else if (count == 0)
                {
                    parentNode.CheckState = CheckState.Unchecked;
                }
                else
                {
                    parentNode.CheckState = CheckState.Indeterminate;
                }
                SetParentNodeState(parentNode);
            }
        }

        /// <summary>
        /// 根据节点状态获取节点。
        /// </summary>
        /// <param name="state">节点状态</param>
        /// <returns></returns>
        public List<TreeListNode> GetAllCheckedNodes(CheckState state = CheckState.Checked)
        {
            List<TreeListNode> nodes = new List<TreeListNode>();
            GetNodes(this.Nodes, nodes, state);
            return nodes;
        }

        private void GetNodes(TreeListNodes nodes, List<TreeListNode> stateListNodes, CheckState state)
        {
            foreach (TreeListNode node in nodes)
            {
                if (node.CheckState == state)
                {
                    stateListNodes.Add(node);
                }
                if (node.HasChildren)
                {
                    GetNodes(node.Nodes, stateListNodes, state);
                }
            }
        }
    }
}
